package com.cssy.xxkj.basic.web;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cssy.base.web.BaseController;
import com.cssy.base.web.BodyMsg;
import com.cssy.xxkj.basic.entity.BasicPersonnelDO;
import com.cssy.xxkj.basic.service.IBasicPersonnelService;
import com.cssy.xxkj.basic.service.IBasicUtilService;
import com.plat.helper.JqGridHandler;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.OperationFlag;
import com.plat.util.SessionUtil;
import com.plat.util.json.OutJsonGrid;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.impl.SyCodeService;

@ Controller
@RequestMapping("/basic/personnel")
public class PersonnelController extends BaseController<BasicPersonnelDO> {
	private static final String MANAGER = "/xxkj/basic/personnel/PersonnelManager";
	private static final String EIDT = "/xxkj/basic/personnel/PersonnelOperation";
	@Resource(name="basicUtilService")
	private IBasicUtilService basicUtilService;
	
	@Resource(name="basicPersonnelService")
	private IBasicPersonnelService basicPersonnelService;

	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	@Override
	public String manager(HttpServletRequest request, Model model)
			throws Exception {
		return MANAGER;
	}

	@Override
	public void list(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		 JqGridHandler hander=new JqGridHandler(request);
		 SearchConditions scs = new SearchConditions();
		 hander.getWheres(scs);
		 scs.setRemovedProperty(null);
		 SearchResult result = basicPersonnelService.findListByExample(scs);
		 OutJsonGrid out=new OutJsonGrid(result);
		 out.printJSon(response);
		
	}

	@Override
	public String operationForm(HttpServletRequest request, @PathVariable String id,
			@PathVariable String flag, Model model) throws Exception {
		if (OperationFlag.ADD.equals(flag)) {
			model.addAttribute("flag", "ADD");
			}
		else{
			BasicPersonnelDO bpd=basicPersonnelService.findListById(id);
			model.addAttribute("personnes",bpd);
			model.addAttribute("flag", "edit");
		}
		 SearchConditions scs = new SearchConditions();
		 scs.setRemovedProperty(null);
		 model.addAttribute("workShop", basicUtilService.resultWorkshop(scs));
		return EIDT;
	}

	@Override
	public Map<String, Object> save(HttpServletRequest request,
			BasicPersonnelDO personnel, BindingResult result, Model model)
			throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			if (personnel.getId() != null && !"".equals(personnel.getId())) {
				basicPersonnelService.update(personnel, SessionUtil.getUser(request));
				bodyMap.isSuccess();
				bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
				bodyMap.configWebId(personnel.getId());
			} else {
				personnel.setZd001Code(syCodeService.getCode("US", "US", SyCode.DATE_YYMMDD, 4));
				BasicPersonnelDO psdo = basicPersonnelService.save(personnel, SessionUtil.getUser(request));
				bodyMap.isSuccess();
				bodyMap.configWebInfor(GlobalResLoader.getOpsuccess());
				bodyMap.configWebId(psdo.getId());
			}
		} catch (Exception e) {
			e.printStackTrace();
			bodyMap.isFail();
			bodyMap.configWebInfor(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
		return bodyMap.modelMap;
	}

	@Override
	public Map<String, Object> delete(HttpServletRequest request,
			HttpServletResponse response, Model model) throws Exception {
		BodyMsg bodyMap = BodyMsg.getInstance();
		try {
			String isDelete = request.getParameter("isDelete");
			String ids = request.getParameter("ids");
			String[] fields=ids.split(",");
			for(String field : fields){
				if(isDelete==null||"0".equals(isDelete)){
					basicPersonnelService.delete(field, SessionUtil.getUser(request));
				}else{
					basicPersonnelService.deleteBizRemoved(field);
				}
			}
			bodyMap.isSuccess(GlobalResLoader.getOpsuccess());
		} catch (Exception e) {
			e.printStackTrace();
			bodyMap.isFail(GlobalResLoader.getOpfailed() + e.getMessage());
		}
		return bodyMap.modelMap;
	}

}
